package com.cxrj.zhsq.userManage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.alibaba.druid.support.json.JSONUtils;
import com.cxrj.zhsq.administrate.dao.PropertyMapper;
import com.cxrj.zhsq.userManage.dao.AppUserMapper;
import com.cxrj.zhsq.userManage.dao.DoorMapper;
import com.cxrj.zhsq.userManage.entity.AppUser;
import com.cxrj.zhsq.userManage.entity.Door;
import com.cxrj.zhsq.userManage.service.DoorService;
import com.cxrj.zhsq.utils.DoorUtil;

@Service
public class DoorServiceImpl implements DoorService {

	@Resource
	private DoorMapper doorMapper;

	@Resource
	private AppUserMapper appUserMapper;

	@Resource
	private PropertyMapper propertyMapper;

	@Override
	public Map<String, Object> selectDoorsPager(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		String str = " 1=1";
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");

		String name = request.getParameter("name");

		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("condition", " where " + str);
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("name", name);
		// 获取数据库总记录数
		String recordsTotal = doorMapper.getDoorCount(sqlMap) + "";
		List<Map<String, Object>> list = doorMapper.selectDoorsPager(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> insertDoors() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		doorMapper.deleteAll();
		String str = DoorUtil.getDoor(1, 400);
		Map<String, Object> map = (Map<String, Object>) JSONUtils.parse(str);
		Map<String, Object> map1 = (Map<String, Object>) JSONUtils.parse(JSONUtils.toJSONString(map.get("data")));
		List<Door> doors = (List<Door>) JSONUtils.parse(JSONUtils.toJSONString(map1.get("list")));
		Integer c = doorMapper.insertDoors(doors);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectDoor(String userId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		AppUser appUser = appUserMapper.selectAppUserById(userId);
		String[] addrs = appUser.getUserAddress().split(",");
		// sqlMap.put("userMobile", appUser.getUserMobile());
		// sqlMap.put("userName", appUser.getUserName());
		// sqlMap.put("userIndentity", appUser.getUserIndentity());
		// List<Property> properties = propertyMapper.selectByPersonnel(sqlMap);
		List<Door> doors = new ArrayList<Door>();
		// for (Property property : properties) {
		// sqlMap.put("str", property.getBuilding() + property.getUnit());
		// doors.add(doorMapper.selectDoor(sqlMap));
		// }
		List<String> queryStrs = new ArrayList<String>();
		for (String string : addrs) {
			String[] str = string.split("-");
			String queryStr = str[0] + str[1];
			queryStrs.add(queryStr);
		}
		List<String> newQuerys = removeDuplicate(queryStrs);
		for (String string : newQuerys) {
			sqlMap.put("name", string);
			doors.add(doorMapper.selectDoor(sqlMap));
		}
		resultMap.put("list", doors);
		return resultMap;
	}

	public static List<String> removeDuplicate(List<String> list) {
		for (int i = 0; i < list.size() - 1; i++) {
			for (int j = list.size() - 1; j > i; j--) {
				if (list.get(j).equals(list.get(i))) {
					list.remove(j);
				}
			}
		}
		return list;
	}

}
